We’re going to look at the Instacart data

library(tidyverse)
## ── Attaching core tidyverse packages ──────────────────────── tidyverse 2.0.0 ──
## ✔ dplyr     1.1.4     ✔ readr     2.1.5
## ✔ forcats   1.0.0     ✔ stringr   1.5.1
## ✔ ggplot2   3.5.2     ✔ tibble    3.3.0
## ✔ lubridate 1.9.4     ✔ tidyr     1.3.1
## ✔ purrr     1.1.0     
## ── Conflicts ────────────────────────────────────────── tidyverse_conflicts() ──
## ✖ dplyr::filter() masks stats::filter()
## ✖ dplyr::lag()    masks stats::lag()
## ℹ Use the conflicted package (<http://conflicted.r-lib.org/>) to force all conflicts to become errors
library(p8105.datasets)
library(plotly)
## 
## Attaching package: 'plotly'
## 
## The following object is masked from 'package:ggplot2':
## 
##     last_plot
## 
## The following object is masked from 'package:stats':
## 
##     filter
## 
## The following object is masked from 'package:graphics':
## 
##     layout
data("instacart")

instacart_df = instacart

instacart_df =
  instacart %>% 
  mutate(
    department = as.factor(department),
    aisle = as.factor(aisle),
    order_dow = factor(order_dow,
                       levels = 0:6,
                       labels = c("Sun","Mon","Tue","Wed","Thu","Fri","Sat"))) %>%
  sample_n(1000)

Bar Plot

instacart_bar =
instacart_df %>%
  count(department) %>%
  mutate(department = fct_reorder(department, n)) %>%
  plot_ly(
    x = ~department,
    y = ~n,
    color = ~department,
    type = "bar",
    colors = "viridis") %>%
  layout(
    title = "Most Frequently Ordered Departments",
    xaxis = list(title = "Department"),
    yaxis = list(title = "Number of Products Ordered")
  )

instacart_bar

Box Plot

instacart_box = 
  instacart %>%
  filter(department %in% c("produce", "snacks", "dairy eggs", "frozen", "beverages")) %>%
  mutate(department = fct_reorder(department, order_hour_of_day)) %>% 
  plot_ly(
    x = ~department,
    y = ~order_hour_of_day,
    color = ~department,
    type = "box",
    colors = "viridis") %>% 
  layout(
    title = "Distribution of Order Hour by Top 5 Departments",
    xaxis = list(title = "Department"),
    yaxis = list(title = "Hour of Day Order was Placed")
  )

instacart_box

Line Plot

instacart_line =
  instacart_df %>%
  group_by(order_dow) %>%                              
  summarise(avg_hour = mean(order_hour_of_day, na.rm = TRUE)) %>%  
  plot_ly(
    x = ~order_dow,
    y = ~avg_hour,
    type = "scatter",
    mode = "lines+markers",
    line = list(color = "black"),
    marker = list(size = 8)
  ) %>%
  layout(
    title = "Average Order Hour by Day of Week",
    xaxis = list(title = "Day of Week"),
    yaxis = list(title = "Average Hour of Day (0–23)"))

instacart_line